package com.wyl.server.publish

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.PublishTypePage
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.binding.recyclerview.RecyclerViewSpace
import com.wyl.libbase.extend.openActivity
import com.wyl.server.R
import com.wyl.server.databinding.PublishTypeActivityBinding
import io.ditclear.bindingadapter.ItemClickPresenter
import io.ditclear.bindingadapter.SingleTypeAdapter
import org.koin.android.viewmodel.ext.android.viewModel

@Route(path = PublishTypePage)
class PublishTypeActivity : BindingActivity<PublishTypeActivityBinding>() {

    private val viewModel by viewModel<PublishTypeViewModel>()

    override fun getLayoutId(): Int = R.layout.publish_type_activity

    override fun initView() {
        binding.vm = viewModel

        binding.titlebar.apply {
            setListener { _, action, _ ->
                when (action) {
                    CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                    else -> {
                    }
                }
            }
        }

        binding.include.recyclerView.apply {
            layoutManager = GridLayoutManager(context, 3)
            adapter = mAdapter
            addItemDecoration(RecyclerViewSpace(30, 30))
        }

    }

    private val mAdapter by lazy {
        SingleTypeAdapter(this, R.layout.service_item1, viewModel.dataSource)
            .apply {
                itemPresenter = object : ItemClickPresenter<Any> {
                    override fun onItemClick(v: View, item: Any) {
                        openActivity(PublishActivity::class.java) {
                            putExtra("id", "123")
                            putExtra("type", "洗车服务")
                        }
                    }
                }
            }
    }

    override fun loadData() {
        binding.include.refreshLayout.startRefresh()
    }
}
